<!-- 修改 木遥（微信:  http://marsgis.cn/weixin.html ） -->
<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0,minimum-scale=1.0,maximum-scale=1.0" />
    <meta name="apple-touch-fullscreen" content="yes" />
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black" />
    <meta name="format-detection" content="telephone=no" />
    <meta name="x5-fullscreen" content="true" />
    <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1" />

    <!-- 标题及搜索关键字 -->
    <meta name="keywords" content="火星科技,cesium,3D,GIS,marsgis,三维,地球,地图,开发,框架,系统,示例,资料,模型,离线,外包,合肥,安徽,中国" />
    <meta
      name="description"
      content="火星科技 合肥火星 合肥火星科技 合肥火星科技有限公司 leaflet leaflet框架 leaflet开发 cesium cesium开发 cesium框架 三维 地球 模型  gis marsgis 地图离线 地图开发 地图框架 地图外包 框架 开发 外包  地图离线 二维地图 三维地图 全景漫游 地理信息系统 云GIS 三维GIS GIS平台 WebGIS"
    />

    <link rel="shortcut icon" type="image/x-icon" href="http://mars3d.cn/favicon.ico" />
    <title>KML/KMZ解析加载(转GeoJson方式) | Mars3D | 三维地图 | 火星科技 | 合肥火星科技有限公司</title>

    <script
      type="text/javascript"
      src="../lib/include-lib.js"
      libpath="../lib/"
      include="jquery,font-awesome,bootstrap,layer,haoutil,ztree,turf,mars3d"
    ></script>

    <!-- KML/KMZ 转 GeoJSON 格式 -->
    <script type="text/javascript" src="../lib/kml/kml-geojson.js"></script>
    <!-- Kml2JsonLayer图层封装 -->
    <script type="text/javascript" src="./js/Kml2JsonLayer.js"></script>

    <link href="css/style.css" rel="stylesheet" />
  </head>

  <body class="dark">
    <!--加载前进行操作提示，优化用户体验-->
    <div id="mask" class="signmask" onclick="removeMask()"></div>

    <div id="mars3dContainer" class="mars3d-container"></div>

    <div id="viewSL" class="infoview">
      示例：
      <input type="button" class="btn btn-primary" value="铁路" onclick="showTieluDemo()" />
      <input type="button" class="btn btn-primary" value="高速公路线" onclick="showGaosuluDemo()" />
      <input type="button" class="btn btn-primary" value="气象等值面" onclick="showQixiangDemo()" />

      <input type="button" class="btn btn-primary" value="国家GDP数据" onclick="showGuojiaDemo()" />
      <input type="button" class="btn btn-primary" value="海上安全通告" onclick="showAnquanDemo()" />
    </div>

    <div class="infoview" style="overflow: auto; right: 5px; left: auto; top: 50px; max-height: 90%">
      <ul id="treeOverlays" class="ztree"></ul>
    </div>

    <script src="./js/common.js"></script>
    <script type="text/javascript">
      "use script"; //开发环境建议开启严格模式

      var map;

      function initMap(options) {
        //合并属性参数，可覆盖config.json中的对应配置
        var mapOptions = mars3d.Util.merge(options, {
          scene: {
            center: { lat: 31.614035, lng: 117.292184, alt: 25686, heading: 0, pitch: -44 },
          },
          layers: [
            {
              name: "国境线",
              type: "kml2json",
              url: "//data.mars3d.cn/file/kml/countryboundary.kml",
              symbol: {
                styleOptions: {
                  color: "#FED976",
                  width: 2,
                },
              },
              popup: "all",
              show: true,
            },
            {
              name: "省界线",
              type: "kml2json",
              url: "//data.mars3d.cn/file/kml/province.kml",
              symbol: {
                styleOptions: {
                  color: "#00FF00",
                  width: 2,
                },
              },
              popup: "all",
              show: true,
            },
          ],
        });

        //创建三维地球场景
        map = new mars3d.Map("mars3dContainer", mapOptions);

        showTieluDemo();
      }

      var kmlLayer;

      function removeLayer() {
        map.trackedEntity = null;
        if (kmlLayer) {
          map.removeLayer(kmlLayer, true);
          kmlLayer = null;
        }
      }

      //示例：
      function showTieluDemo() {
        removeLayer();

        kmlLayer = new mars3d.layer.Kml2JsonLayer({
          url: "//data.mars3d.cn/file/kml/hftl.kml",
          symbol: {
            styleOptions: {
              color: "#00ffff",
              opacity: 0.8,
              width: 3,
              arcType: Cesium.ArcType.GEODESIC,
              clampToGround: true,
              //标记文字
              label: {
                text: "{name}",
                opacity: 1,
                font_size: 30,
                color: "#ffffff",

                font_family: "楷体",
                outline: true,
                outlineColor: "#000000",
                outlineWidth: 3,

                scaleByDistance: true,
                scaleByDistance_far: 20000,
                scaleByDistance_farValue: 0.1,
                scaleByDistance_near: 100,
                scaleByDistance_nearValue: 1,
                distanceDisplayCondition: new Cesium.DistanceDisplayCondition(0.0, 20000),
              },
            },
          },
          center: { lat: 31.653222, lng: 117.273592, alt: 35183, heading: 358, pitch: -63 },
          popup: "all",
          flyTo: true,
        });
        map.addLayer(kmlLayer);

        //绑定事件
        kmlLayer.on(mars3d.EventType.load, function (event) {
          console.log("数据加载完成", event);
          initTree(event.list);
        });
        kmlLayer.on(mars3d.EventType.click, function (event) {
          console.log("单击了图层", event);
        });
      }

      //示例：
      function showGaosuluDemo() {
        removeLayer();

        kmlLayer = new mars3d.layer.Kml2JsonLayer({
          name: "路线",
          url: "//data.mars3d.cn/file/kml/bslx.kmz",
          popup: "all",
          flyTo: true,
        });
        map.addLayer(kmlLayer);

        //绑定事件
        kmlLayer.on(mars3d.EventType.load, function (event) {
          console.log("数据加载完成", event);
          initTree(event.list);
        });
        kmlLayer.on(mars3d.EventType.click, function (event) {
          console.log("单击了图层", event);
        });
      }

      //示例：
      function showAnquanDemo() {
        removeLayer();

        kmlLayer = new mars3d.layer.Kml2JsonLayer({
          name: "海上安全警告",
          url: "//data.mars3d.cn/file/kml/NAVWARN.kmz",
          symbol: function (attr, style, featue) {
            let geoType = featue.geometry?.type;
            if (geoType == "Point") {
              return {
                image: "img/marker/di3.png",
                verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
                scale: 0.4,
                label: {
                  text: attr.name,
                  font_size: 18,
                  color: "#ffffff",
                  outline: true,
                  outlineColor: "#000000",
                  pixelOffsetY: -50,
                  scaleByDistance: true,
                  scaleByDistance_far: 990000,
                  scaleByDistance_farValue: 0.3,
                  scaleByDistance_near: 10000,
                  scaleByDistance_nearValue: 1,
                },
              };
            }
            return style;
          },
          popup: "{description}",
          center: { lat: 4.112702, lng: 110.383709, alt: 3269095, heading: 7, pitch: -74 },
          flyTo: true,
        });
        map.addLayer(kmlLayer);

        //绑定事件
        kmlLayer.on(mars3d.EventType.load, function (event) {
          console.log("数据加载完成", event);
          initTree(event.list);
        });
        kmlLayer.on(mars3d.EventType.click, function (event) {
          console.log("单击了图层", event);
        });
      }

      //示例：
      function showQixiangDemo() {
        removeLayer();

        kmlLayer = new mars3d.layer.Kml2JsonLayer({
          name: "气象数据",
          url: "//data.mars3d.cn/file/kml/dg8.kml",
          opacity: 0.7,
          popup: "all",
          flyTo: true,
        });
        map.addLayer(kmlLayer);

        //绑定事件
        kmlLayer.on(mars3d.EventType.load, function (event) {
          console.log("数据加载完成", event);
          initTree(event.list);
        });
        kmlLayer.on(mars3d.EventType.click, function (event) {
          console.log("单击了图层", event);
        });
      }

      //示例：
      function showGuojiaDemo() {
        removeLayer();

        kmlLayer = new mars3d.layer.Kml2JsonLayer({
          name: "全球各国GDP",
          url: "//data.mars3d.cn/file/kml/gdpPerCapita2008.kmz",
          symbol: function (attr, style, featue) {
            let geoType = featue.geometry?.type;
            if (geoType == "Point") {
              return {
                type: "label", //指定用label渲染。
                text: attr.name,
                font_size: 18,
                color: "#ffffff",
                outline: true,
                outlineColor: "#000000",
              };
            } else if (geoType == "Polygon") {
              style.extrudedHeight = 1;
              // style.diffHeight = featue.geometry?.coordinates[0][0][2]
            }

            return style;
          },
          center: { lat: 12.46821, lng: 91.404177, alt: 18969935, heading: 359, pitch: -87 },
          popup: "all",
          flyTo: true,
        });
        map.addLayer(kmlLayer);

        //绑定事件
        kmlLayer.on(mars3d.EventType.load, function (event) {
          console.log("数据加载完成", event);
          initTree(event.list);
        });
        kmlLayer.on(mars3d.EventType.click, function (event) {
          console.log("单击了图层", event);
        });
      }

      //===========================树控件处理============================
      var layersObj = {};

      function initTree(arr, nameColum = "name") {
        //初始化树
        var setting = {
          check: {
            enable: true,
          },
          data: {
            simpleData: {
              enable: true,
            },
          },
          callback: {
            onCheck: treeOverlays_onCheck,
            onClick: treeOverlays_onClick,
          },
        };

        var zNodes = [];
        var pnode = {
          id: -1,
          name: "全部",
          type: "group",
          open: false,
          checked: true,
          icon: "../lib/jquery/ztree/css/mars/images/folder.png",
        };
        zNodes.push(pnode);

        for (var i = 0, len = arr.length; i < len; i++) {
          var item = arr[i];
          var name = item[nameColum] || "未命名";

          var node = {
            id: i,
            pId: pnode.id,
            name: name,
            checked: true,
            icon: "../lib/jquery/ztree/css/mars/images/layer.png",
          };
          zNodes.push(node);

          layersObj[i] = item.graphic;
        }

        $.fn.zTree.destroy();
        $.fn.zTree.init($("#treeOverlays"), setting, zNodes);
      }

      function treeOverlays_onCheck(e, treeId) {
        var zTree = $.fn.zTree.getZTreeObj(treeId);

        //获得所有改变check状态的节点
        var changedNodes = zTree.getChangeCheckedNodes();
        if (changedNodes) {
          for (var i = 0; i < changedNodes.length; i++) {
            var treeNode = changedNodes[i];
            treeNode.checkedOld = treeNode.checked;
            var graphic = layersObj[treeNode.id];
            if (graphic == null) {
              continue;
            }
            let show = treeNode.checked;
            //处理图层显示隐藏
            graphic.show = show;
          }
        }
      }

      function treeOverlays_onClick(event, treeId, treeNode) {
        var graphic = layersObj[treeNode.id];
        if (graphic == null) {
          return;
        }

        map.flyToGraphic(graphic);
      }
    </script>
  </body>
</html>
